package com.kusen.village.raise.controller;

import com.kusen.village.raise.entity.jsonbean.HopefulJsonDto;
import com.kusen.village.raise.util.JsonUtil;
import com.kusen.village.raise.util.send.MsgWriter;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.integration.core.MessageProducer;
import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *控制硬件===向mqtt发送topic的数据
 */
@Api(value = "发送topic 主题的")
@RestController
public class VillageController {
    @Autowired
    private MsgWriter msgWriter;

    @Qualifier("inbound")
    @Autowired
    private MessageProducer messageProducer;

    @GetMapping("/getMes")
    public String getMes() {
        return "hello world";
    }

    /**
     * 控制电机运转
     * @return
     */
    @PostMapping("/sendMes")
    public String sendMes(String state,String topic) {
        HopefulJsonDto hopefulJsonDto=new HopefulJsonDto();
        hopefulJsonDto.setData(state);
        hopefulJsonDto.setType("1");
        String json = JsonUtil.getJson(hopefulJsonDto);
        msgWriter.sendToMqtt(json, topic);
        return "1";
    }

    /**
     * 控制灯泡工作
     * @param state
     * @param topic
     * @return
     */
    @PostMapping("/sendMesLight")
    public String sendMesLight(String state,String topic){
        msgWriter.sendToMqtt(state,topic);
        return "1";
    }

    /**
     * 添加监听的topic
     * @param topic
     * @return
     */
    @PostMapping("/addTopic")
    public String addTopic(String topic){
        MqttPahoMessageDrivenChannelAdapter adapter= (MqttPahoMessageDrivenChannelAdapter) messageProducer;
        adapter.addTopic(topic);
        return "1";
    }

    @PostMapping("/textAjax")
    public String textAjax(String topic,String data){
        System.out.println(topic+data);
    return "1";
    }

}
